package com.example.database;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class Calculate extends AppCompatActivity implements View.OnClickListener {

    EditText et;
    TextView tv_1, tv_2, tv_3, tv_4, tv_5, tv_6, tv_7, tv_8, tv_9, tv_0,
            tv_C, tv_chu, tv_mul, tv_delete, tv_jian, tv_jia, tv_deng, tv_bf, tv_dot;

    double num1 = 0, num2 = 0;
    double result = 0;
    int op = 0;
    boolean isClickEqu = false;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_calculate_grid);


        et = (EditText) findViewById(R.id.et);

        tv_0 = (TextView) findViewById(R.id.tv_0);
        tv_1 = (TextView) findViewById(R.id.tv_1);
        tv_2 = (TextView) findViewById(R.id.tv_2);
        tv_3 = (TextView) findViewById(R.id.tv_3);
        tv_4 = (TextView) findViewById(R.id.tv_4);
        tv_5 = (TextView) findViewById(R.id.tv_5);
        tv_6 = (TextView) findViewById(R.id.tv_6);
        tv_7 = (TextView) findViewById(R.id.tv_7);
        tv_8 = (TextView) findViewById(R.id.tv_8);
        tv_9 = (TextView) findViewById(R.id.tv_9);
        tv_jia = (TextView) findViewById(R.id.tv_jia);
//        tv_bf = (TextView) findViewById(R.id.tv_bf);
        tv_mul = (TextView) findViewById(R.id.tv_mul);
        tv_chu = (TextView) findViewById(R.id.tv_chu);
        tv_dot = (TextView) findViewById(R.id.tv_dot);
        tv_deng = (TextView) findViewById(R.id.tv_deng);
        tv_delete = (TextView) findViewById(R.id.tv_delete);
        tv_jian = (TextView) findViewById(R.id.tv_jian);
        tv_C = (TextView) findViewById(R.id.tv_C);

        tv_0.setOnClickListener(this);
        tv_1.setOnClickListener(this);
        tv_2.setOnClickListener(this);
        tv_3.setOnClickListener(this);
        tv_4.setOnClickListener(this);
        tv_5.setOnClickListener(this);
        tv_6.setOnClickListener(this);
        tv_7.setOnClickListener(this);
        tv_8.setOnClickListener(this);
        tv_9.setOnClickListener(this);
        tv_jia.setOnClickListener(this);
        tv_jian.setOnClickListener(this);
        tv_mul.setOnClickListener(this);
        tv_chu.setOnClickListener(this);
        tv_dot.setOnClickListener(this);
        tv_delete.setOnClickListener(this);
//        tv_bf.setOnClickListener(this);
        tv_deng.setOnClickListener(this);
        tv_C.setOnClickListener(this);

    }

    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_0:
                if (isClickEqu) {
                    et.setText(null);
                    isClickEqu = false;
                }
                String str0 = et.getText().toString();
                str0 += "0";
                et.setText(str0);
                break;
            case R.id.tv_1:
                if (isClickEqu) {
                    et.setText(null);
                    isClickEqu = false;
                }
                String str1 = et.getText().toString();
                str1 += "1";
                et.setText(str1);
                break;
            case R.id.tv_2:
                if (isClickEqu) {
                    et.setText(null);
                    isClickEqu = false;
                }
                String str2 = et.getText().toString();
                str2 += "2";
                et.setText(str2);
                break;
            case R.id.tv_3:
                if (isClickEqu) {
                    et.setText(null);
                    isClickEqu = false;
                }
                String str3 = et.getText().toString();
                str3 += "3";
                et.setText(str3);
                break;
            case R.id.tv_4:
                if (isClickEqu) {
                    et.setText(null);
                    isClickEqu = false;
                }
                String str4 = et.getText().toString();
                str4 += "4";
                et.setText(str4);
                break;
            case R.id.tv_5:
                if (isClickEqu) {
                    et.setText(null);
                    isClickEqu = false;
                }
                String str5 = et.getText().toString();
                str5 += "5";
                et.setText(str5);
                break;
            case R.id.tv_6:
                if (isClickEqu) {
                    et.setText(null);
                    isClickEqu = false;
                }
                String str6 = et.getText().toString();
                str6 += "6";
                et.setText(str6);
                break;
            case R.id.tv_7:
                if (isClickEqu) {
                    et.setText(null);
                    isClickEqu = false;
                }
                String str7 = et.getText().toString();
                str7 += "7";
                et.setText(str7);
                break;
            case R.id.tv_8:
                if (isClickEqu) {
                    et.setText(null);
                    isClickEqu = false;
                }
                String str8 = et.getText().toString();
                str8 += "8";
                et.setText(str8);
                break;
            case R.id.tv_9:
                if (isClickEqu) {
                    et.setText(null);
                    isClickEqu = false;
                }
                String str9 = et.getText().toString();
                str9 += "9";
                et.setText(str9);
                break;
            case R.id.tv_dot:
                if (isClickEqu) {
                    et.setText(null);
                    isClickEqu = false;
                }
                String strdot = et.getText().toString();
                strdot += ".";
                et.setText(strdot);
                break;

            case R.id.tv_C:
                et.setText(null);
                break;

            case R.id.tv_delete:
                String str = et.getText().toString();
                if (str.length() > 0) {
                    str = str.substring(0, str.length() - 1);
                    et.setText(str);
                }
                break;

            case R.id.tv_jia:
                String stradd = et.getText().toString();
                if (stradd.equals(null))
                    return;
                num1 = Double.valueOf(stradd);
                et.setText(null);
                op = 1;
                isClickEqu = false;
                break;

            case R.id.tv_jian:
                String strsub = et.getText().toString();
                if (strsub.equals(null))
                    return;
                num1 = Double.valueOf(strsub);
                et.setText(null);
                op = 2;
                isClickEqu = false;
                break;

            case R.id.tv_mul:
                String strmul = et.getText().toString();
                if (strmul.equals(null))
                    return;
                num1 = Double.valueOf(strmul);
                et.setText(null);
                op = 3;
                isClickEqu = false;
                break;

            case R.id.tv_chu:
                String strdiv = et.getText().toString();
                if (strdiv.equals(null))
                    return;
                num1 = Double.valueOf(strdiv);
                et.setText(null);
                op = 4;
                isClickEqu = false;
                break;

//            case R.id.tv_bf:
//                String strbfs = et.getText().toString();
//                if (strbfs.equals(null))
//                    return;
//                num1 = Double.valueOf(strbfs);
//                et.setText(null);
//                op = 5;
//                isClickEqu = false;
//                break;


            case R.id.tv_deng:
                String strequ = et.getText().toString();
                if (strequ.equals(null))
                    return;
                num2 = Double.valueOf(strequ);
                et.setText(null);
                switch (op) {
                    case 0:
                        result = num2;
                        break;
                    case 1:
                        result = num1 + num2;
                        break;
                    case 2:
                        result = num1 - num2;
                        break;
                    case 3:
                        result = num1 * num2;
                        break;
                    case 4:
                        result = num1 / num2;
                        break;


                    default:
                        result = 0;
                        break;
                }
                et.setText(String.valueOf(result));
                isClickEqu = true;
                break;
            default:
                break;


        }

    }
}